<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitealbum
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: Controller.php 2011-08-026 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitealbum_Widget_PhotoStripsController extends Engine_Content_Widget_Abstract {

  public function indexAction() {
    // DON'T RENDER IF SUNJECT IS NOT THERE
    if (!Engine_Api::_()->core()->hasSubject('user')) {
      return $this->setNoRender();
    }

    // GET SUBJECT
    $subject = Engine_Api::_()->core()->getSubject('user');

    $owner_id = $subject->getIdentity();
    // Should we consider creation or modified recent?
    $recentType = $this->_getParam('recentType', 'creation');
    if (!in_array($recentType, array('creation', 'modified'))) {
      $recentType = 'creation';
    }
    $this->view->limit = $limit = $this->_getParam('itemCountPerPage', 7);
    $this->view->recentType = $recentType;
    $this->view->recentCol = $recentCol = $recentType . '_date';
    $parentTable = Engine_Api::_()->getItemTable('album');
    $table = Engine_Api::_()->getItemTable('album_photo');
    $tableName = $table->info('name');
    //CHECK REQUEST IS AJAX OR NOT
    $this->view->is_ajax = $is_ajax = $this->_getParam('isajax', '');
    $hide_photo_id = $this->_getParam('hide_photo_id', null);
    if (!empty($is_ajax) && !empty($hide_photo_id)) {
      $table->update(array('photo_hide' => 1), array('photo_id = ?' => $this->_getParam('hide_photo_id', null)));
    }

    // Get paginator
    $parentTableName = $parentTable->info('name');
    $select = $table->select()
            ->from($tableName);
    if (!Engine_Api::_()->sitealbum()->isLessThan417AlbumModule()) {
      $select->joinLeft($parentTableName, $parentTableName . '.album_id=' . $tableName . '.album_id', null);
    } else {
      $select->joinLeft($parentTableName, $parentTableName . '.album_id=' . $tableName . '.collection_id', null);
    }
    $select->where($tableName . '.owner_id = ?', $owner_id)
            ->where($tableName . '.photo_hide = ?', 0)
            ->where('search = ?', true);
    if (!Engine_Api::_()->sitealbum()->canShowSpecialAlbum())
      $select->where($parentTableName . '.type IS NULL');
    $select->order($tableName . '.' . $recentCol . ' DESC');
    $select->limit($limit);
    $this->view->paginator = $paginator = $paginator = Zend_Paginator::factory($select);
    $paginator->setItemCountPerPage($limit);
    $paginator->setCurrentPageNumber($this->_getParam('page', 1));
    $this->view->count = $count = $paginator->getTotalItemCount();
    $this->view->showLightBox = Engine_Api::_()->sitealbum()->showLightBoxPhoto();
    $viewer = Engine_Api::_()->user()->getViewer();
    $canEdit = Engine_Api::_()->authorization()->isAllowed(null, $viewer, 'edit');
    $this->view->canEdit = $subject->isSelf($viewer);
    if (!$this->view->canEdit) {
      $viewer_id = $viewer->getIdentity();
      if (!empty($viewer_id) && $viewer->level_id == 1)
        $this->view->canEdit = true;
    }
    $this->view->hidePhoto = 0;
    if ($this->view->canEdit) {
      $hidePhoto = $table->fetchRow(array('photo_hide = ?' => 1, 'owner_id = ?' => $owner_id));
      $this->view->hidePhoto = $hidePhotoCount = count($hidePhoto);
      if (empty($count))
        $count = $hidePhotoCount;
    }
    // Do not render if nothing to show
    /* if ($count <= 0) {
      return $this->setNoRender();
      } */
    $this->view->count = $count;
    if ($this->view->showLightBox) {
      $this->view->params = $params = array('type' => 'strip_' . $recentType, 'count' => $count, 'owner_id' => $owner_id);
    }
  }

}

?>
